<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header height="50px">
      <div @click="goHome" class="homeLogo">
        <img src="./../assets/head.jpg" class="avatar" alt="" srcset="">
        <span>电商后台管理系统</span>
      </div>
      <!-- <el-link></el-link> -->
      <el-button type="danger" @click="login_out">退出</el-button>
      <!-- Header content -->
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧栏 -->

      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- Aside content -->
        <el-menu
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409FFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 二级菜单 -->
          <el-submenu
            :index="'a' + item.id"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <el-menu-item-group>
              <!-- <template slot="title">分组一</template> -->
              <el-menu-item
                :index="'/' + SubItem.path"
                v-for="SubItem in item.children"
                :key="SubItem.id"
                @click="saveNavState('/' + SubItem.path)"
                ><i class="el-icon-menu" t></i
                >{{ SubItem.authName }}</el-menu-item
              >
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边区域 -->

      <el-main height="100%">
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      headImg: './../assets/head.jpg',
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  methods: {
    goHome: function() {
      window.sessionStorage.removeItem('activePath')
      this.$router.push({ path: '/' })
    },
    login_out() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    handleOpen(key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath)
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message.error('获取菜单失败')
      }
      this.menuList = res.data
      // console.log(res)
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scope>
  .avatar{
    border-radius: 3px;
  }
  .homeLogo{
    cursor: pointer;
  }
.home-container {
  height: 100%;
}
.el-header {
  background-color: rgb(61, 61, 61);
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  img {
    width: 40px;
  }
}
.el-aside {
  background-color: rgb(84, 92, 99);
}
.el-main {
  background-color: #e9e7e7;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none !important;
}
.toggle-button {
  background-color: rgb(54, 57, 59);
  line-height: 24px;
  color: #fff;
  font-size: 10px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.toggle-button:hover {
  background-color: cadetblue;
}
</style>
